//
//  StarEvaluatorView.m
//  StarStore
//
//  Created by 李亚军 on 2017/6/6.
//  Copyright © 2017年 李亚军. All rights reserved.
//

#import "StarEvaluatorView.h"


@interface StarEvaluatorView ()


/// 点击的坐标 X
@property(nonatomic , assign) CGFloat touchX;

/// 不得分的背景视图
@property(nonatomic , strong) UIView *unValueBackView;
/// 没有得分的 imageView 的缓冲池
@property(nonatomic , strong) NSMutableArray *unValueImageViewArray;

/// 得分的背景视图
@property(nonatomic , strong) UIView *valueBackView;
/// 得分的 imageView 的缓存池
@property(nonatomic , strong) NSMutableArray *valueImageViewArray;

/// 显示分数的遮罩视图
@property(nonatomic , strong) CAShapeLayer *valueBackViewShadeLayer;

@end

@implementation StarEvaluatorView
- (NSMutableArray *)unValueImageViewArray {
    if (!_unValueImageViewArray) {
        self.unValueImageViewArray = [NSMutableArray array];
    }
    return _unValueImageViewArray;
}

- (NSMutableArray *)valueImageViewArray {
    if (!_valueImageViewArray) {
        self.valueImageViewArray = [NSMutableArray array];
    }
    return _valueImageViewArray;
}

- (CAShapeLayer *)valueBackViewShadeLayer {
    if (!_valueBackViewShadeLayer) {
        self.valueBackViewShadeLayer = [CAShapeLayer layer];
    }
    return _valueBackViewShadeLayer;
}


#pragma mark - 初始化默认属性
- (instancetype)initWithFrame:(CGRect)frame withNumberOfStar:(NSInteger)num {
    self = [super initWithFrame:frame];
    if (self) {
        
        /// 初始化默认属性
        [self initDefaultsAttributes];
        
        self.starNum = num;
    }

    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self initDefaultsAttributes];
    
    self.starNum = 5;
    
}

/// 初始化默认属性
- (void)initDefaultsAttributes {
    
    self.backgroundColor = [UIColor clearColor];
    self.aspectRatio = 1.0;
    self.isAllowedSliding = YES;
    
    self.layer.masksToBounds = YES;
    
    self.touchX = 0.0;
    
    self.currentValue = 0.0;
    
    
    self.unValueBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width,self.frame.size.height)];
    self.unValueBackView.backgroundColor = [UIColor clearColor];
    [self addSubview:self.unValueBackView];
    
    self.valueBackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.touchX,self.frame.size.height)];
    self.valueBackView.backgroundColor = [UIColor redColor];
    [self addSubview:self.valueBackView];

    self.valueBackView.layer.mask = self.valueBackViewShadeLayer;
    
    [self reloadData];
}


/// 创建 imageView
- (void)createImageView {
    
    NSBundle *podBundle = [NSBundle bundleForClass:[self class]];
    NSURL *bundleURL = [podBundle URLForResource:@"StarRating" withExtension:@"bundle"];
    
    // 这里清除所有的子视图
    [self.unValueBackView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.unValueImageViewArray removeAllObjects];
    [self.valueBackView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.valueImageViewArray removeAllObjects];
    
    
    if (self.starNum == 0) {
        
        return;
    }
    
    CGFloat width = (self.frame.size.width - (self.starNum - 1) * self.horizontalSpacing) / self.starNum;
    CGFloat height = width * self.aspectRatio;
    
    for (NSInteger i = 0; i < self.starNum; i ++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * (width + self.horizontalSpacing), (self.frame.size.height - height) / 2.0, width, height)];
        imageView.backgroundColor = [UIColor clearColor];
        
        if (self.unHightlightImage) {
            
            imageView.image = self.unHightlightImage;
        }else {
            
            imageView.image = [UIImage imageNamed:@"unHighlightImage" inBundle:[NSBundle bundleWithURL:bundleURL] compatibleWithTraitCollection:nil];
        }
        
        [self.unValueBackView addSubview:imageView];
        [self.unValueImageViewArray addObject:imageView];
    }
    
    for (NSInteger i = 0; i < self.starNum; i ++) {
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * (width + self.horizontalSpacing), (self.frame.size.height - height) / 2.0, width, height)];
        imageView.backgroundColor = [UIColor clearColor];
        
        if (self.highlightImage) {
            
            imageView.image = self.highlightImage;
        }else {
            
            imageView.image = [UIImage imageNamed:@"highlightImage" inBundle:[NSBundle bundleWithURL:bundleURL] compatibleWithTraitCollection:nil];
        }

    
        [self.valueBackView addSubview:imageView];
        [self.valueImageViewArray addObject:imageView];
    }
}

/// 重新排序 imageView 的视图位置
- (void)refreshFrame {
    
    if (self.unValueImageViewArray.count == 0 || self.valueImageViewArray.count == 0) {
        
        return;
    }
    
    CGFloat width = (self.frame.size.width - (self.starNum - 1) * self.horizontalSpacing) / self.starNum;
    CGFloat height = width * self.aspectRatio;

    
    if (self.unValueImageViewArray.count > 0) {
        NSInteger i = 0;
        for (UIImageView *imageView in self.unValueImageViewArray) {
            
            imageView.frame = CGRectMake(i * (width + self.horizontalSpacing), (self.frame.size.height - height) / 2.0, width, height);
            i ++;
        }
    }
    
    if (self.valueImageViewArray.count > 0) {
        
        NSInteger i = 0;
        for (UIImageView *imageView in self.valueImageViewArray) {
            
            imageView.frame = CGRectMake(i * (width + self.horizontalSpacing), (self.frame.size.height - height) / 2.0, width, height);
            i ++;
        }
    }
}


#pragma mark - 点击触发事件
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *anyTouch = [touches anyObject];
    
    // 自身点击的位置
    CGPoint touchPoint = [anyTouch locationInView:self];
    
    self.touchX = touchPoint.x;
    
    if (self.isAllowedSliding) {
        
        [self conversionXToValue];
    }else {
        
        [self conversionValueToInt];
    }
}

#pragma mark - 滑动触发事件
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    UITouch *anyTouch = [touches anyObject];
    
    if (!self.isAllowedSliding) {
        
        return;
    }
    
    // 自身点击的位置
    CGPoint touchPoint = [anyTouch locationInView:self];
    
    self.touchX = touchPoint.x;
    
    [self conversionXToValue];
    
}

- (void)refreshLayerPath {
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:CGPointMake(0, 0)];
    [path addLineToPoint:CGPointMake(self.touchX, 0)];
    [path addLineToPoint:CGPointMake(self.touchX, self.frame.size.height)];
    [path addLineToPoint:CGPointMake(0, self.frame.size.height)];
    [path addLineToPoint:CGPointMake(0, 0)];
    
    self.valueBackViewShadeLayer.path = path.CGPath;
}



/// 把点击的坐标转化为当前分数
- (void)conversionXToValue {
    
    CGFloat width = (self.frame.size.width - (self.starNum - 1) * self.horizontalSpacing) / self.starNum;
    
    CGFloat currentValue = 0.0;
    
    if (self.touchX < 0) {
        
        self.touchX = 0;
    }
    
    if (self.touchX > self.frame.size.width) {
        
        self.touchX = self.frame.size.width;
    }
    
    if (self.touchX <= (width + self.horizontalSpacing)) {
        
        if (self.touchX <= width) {
            
            currentValue = self.touchX / width;
        }else {
            
            currentValue = 1.0;
        }
        
    }else {
        
        NSInteger n = self.touchX / (width + self.horizontalSpacing);
        
        CGFloat dif = self.touchX - n * (width + self.horizontalSpacing);
        
        if (dif - width >= 0) {
            
            currentValue = n + 1;
        }else {
            
            currentValue = (n + dif / width);
        }
    }
    
    self.currentValue = currentValue;
    
    if (self.currentValueBlock) {
        
        self.currentValueBlock(self.currentValue);
    }
}

/// 把分数转化为点击的坐标 X
- (void)conversionValueToX {
    
    CGFloat width = (self.frame.size.width - (self.starNum - 1) * self.horizontalSpacing) / self.starNum;
    
    if (self.currentValue <= 1.0) {
        
        self.touchX = self.currentValue * width;
        
        if (self.currentValue <= 0) {
            
            self.touchX = 0.0;
        }
        
    }else {
        
        NSInteger n = self.currentValue;
        
        self.touchX = n * (width + self.horizontalSpacing) + (self.currentValue - n) * width;
    }
    
    if (self.currentValueBlock) {
        
        self.currentValueBlock(self.currentValue);
    }
}


/// 把点击位置的分数转化为整分数
- (void)conversionValueToInt {
    
    CGFloat width = (self.frame.size.width - (self.starNum - 1) * self.horizontalSpacing) / self.starNum;
    
    if (self.touchX <= width) {
        
        if (self.currentValue == 1) {
            
            self.currentValue = 0;
        }else {
            
            self.currentValue = 1;
        }
        
        [self conversionValueToX];
    }else {
        
        NSInteger n = self.touchX / (width + self.horizontalSpacing);
        
        if ((self.touchX >= (width + self.horizontalSpacing) * n) && (self.touchX <= ((width + self.horizontalSpacing) * (n + 1) - self.horizontalSpacing))) { // 如果点击的位置在图形内,设置分数
            
            self.currentValue = n + 1;
            if (self.currentValue >= self.starNum) {
                
                self.currentValue = self.starNum;
            }
            [self conversionValueToX];
        }
    }
}


/// 刷新 UI
- (void)reloadData {
    
    [self createImageView];
    [self refreshFrame];
    [self conversionValueToX];
    [self refreshLayerPath];
}

#pragma mark - 绘图
- (void)drawRect:(CGRect)rect {
    
}

#pragma mark - 重写 setter 方法
- (void)setHorizontalSpacing:(CGFloat)horizontalSpacing {
    
    _horizontalSpacing = horizontalSpacing;
    
    if (_horizontalSpacing <= 0) {
        
        _horizontalSpacing = 0;
    }
    
    [self reloadData];
}

- (void)setFrame:(CGRect)frame {
    
    [super setFrame:frame];
    
    self.unValueBackView.frame = CGRectMake(0, 0, frame.size.width, frame.size.height);
    self.valueBackView.frame = frame;
    
    [self reloadData];
}

- (void)setAspectRatio:(CGFloat)aspectRatio {
    
    _aspectRatio = aspectRatio;
    
    [self reloadData];
}

- (void)setCurrentValue:(CGFloat)currentValue {
    _currentValue = currentValue;
    if (_currentValue >= _starNum) {
        
        _currentValue = _starNum;
    }
    
    [self reloadData];
}

- (void)setStarNum:(NSInteger)starNum {
    
    
    if (starNum < 0) {
        
        _starNum = 0;
    }else {
        
        _starNum = starNum;
    }
    
    [self reloadData];
}


- (void)setHighlightImage:(UIImage *)highlightImage {
    _highlightImage = highlightImage;
    
    [self reloadData];
}

- (void)setUnHightlightImage:(UIImage *)unHightlightImage {
    _unHightlightImage = unHightlightImage;
    
    [self reloadData];
}

@end
